package com.itheima.controller;

import com.itheima.domain.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestDemo {
    //获取Bean对象的三种方式
    @Test
    public void test01() {
        //创建ICO容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
        //第一种方式:强制类型转换
        User user = (User) ac.getBean("u");
        user.eat();
        //第二种方式:字节码类型
        //这种方式如有多个相同类型对象,此时不知道该取哪个对象
        User user1 = ac.getBean(User.class);
        user1.eat();
        //第三种方式
        User user2 = ac.getBean("u", User.class);
        user2.eat();
    }

    @Test
    public void text02() {
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
        //销毁IOC容器
        ac.close();
    }

    //创建Bean对象的三种方式
    @Test
    public void text03() {
      /*  UserFactory uf = new UserFactory();
        User user = uf.getUser();
        user.eat();*/
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
        User u1 = ac.getBean("u1", User.class);
        u1.eat();
    }


    //set赋值
    @Test
    public void text04() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
        User u1 = ac.getBean("u", User.class);
        System.out.println(u1.getName()+u1.getAge());
        System.out.println(u1.getBirthday());
        System.out.println(u1.getAddress().getCname());
    }


}
